package com.deyuanyun.pic.ctrl.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.ctrl.controller.vo.query.EqptQueryVO;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.service.ExternalCrsnEqptService;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.settings.util.DownExcelUtil;

/**
 * 外腐蚀控制设备
 * @author lijun
 * @date 2016-09-12
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/corrctrl/externalCrsnEqpt")
public class ExternalCrsnEqptController extends BaseController {

    @Resource
    private ExternalCrsnEqptService externalCrsnEqptServiceImpl;

    /**
     * 跳转到阴保电源设备
     */
    @RequestMapping("/eqptCppower.html")
    public ModelAndView eqptCppowerInit() {

        return forword("/corrctrl/device/deviceManage");
    }

    /**
     * 查询阴保电源设备
     */
    @RequestMapping("/eqptCppower.json")
    @ResponseBody
    public void queryEqptCppower(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List cppowerList= externalCrsnEqptServiceImpl.queryEqptCppower(eqptQuery);
        jsonBean.setList(cppowerList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd", jsonBean);
    }

    /**
     * 查询长效参比设备
     */
    @RequestMapping("/eqptReference.json")
    @ResponseBody
    public void queryEqptReference(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List referenceList= externalCrsnEqptServiceImpl.queryEqptReference(eqptQuery);
        jsonBean.setList(referenceList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",jsonBean);
    }

    /**
     * 查询阳极地床设备
     */
    @RequestMapping("/eqptAnodesbed.json")
    @ResponseBody
    public void queryEqptAnodesbed(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List anodesbedList= externalCrsnEqptServiceImpl.queryEqptAnodesbed(eqptQuery);
        jsonBean.setList(anodesbedList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",  jsonBean);
    }

    /**
     * 查询绝缘装置设备
     */
    @RequestMapping("/eqptInsulatedJoint.json")
    @ResponseBody
    public void queryEqptInsulatedJoint(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List insulatedJointList= externalCrsnEqptServiceImpl.queryEqptInsulatedJoint(eqptQuery);
        jsonBean.setList(insulatedJointList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",  jsonBean);
    }

    /**
     * 查询防浪涌保护器设备
     */
    @RequestMapping("/eqptAntiSurge.json")
    @ResponseBody
    public void queryEqptAntiSurge(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List antiSurgeList= externalCrsnEqptServiceImpl.queryEqptAntiSurge(eqptQuery);
        jsonBean.setList(antiSurgeList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",  jsonBean);
    }

    /**
     * 查询跨界线设备
     */
    @RequestMapping("/eqptCpJumper.json")
    @ResponseBody
    public void queryEqptCpJumper(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List cpJumperList= externalCrsnEqptServiceImpl.queryEqptCpJumper(eqptQuery);
        jsonBean.setList(cpJumperList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",  jsonBean);
    }


    /**
     * 查询阴保测试桩设备
     */
    @RequestMapping("/eqptCptestStation.json")
    @ResponseBody
    public void queryEqptCptestStation(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List cptestStationList= externalCrsnEqptServiceImpl.queryEqptCptestStation(eqptQuery);
        jsonBean.setList(cptestStationList);
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd",jsonBean);
    }

    /**
     * 查询牺牲阳极设备
     */
    @RequestMapping("/eqptSaalAnode.json")
    @ResponseBody
    public void queryEqptSaalAnode(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List saalAnodeList= externalCrsnEqptServiceImpl.queryEqptSaalAnode(eqptQuery);
        jsonBean.setList(saalAnodeList);
        AjaxSupport.sendSuccessText("success", "yyyy-MM-dd", jsonBean);
    }

    /**
     * 查询排流装置设备
     */
    @RequestMapping("/eqptCurrentDrainage.json")
    @ResponseBody
    public void queryEqptCurrentDrainage(EqptQueryVO eqptQuery) {
        //分页
        PageJsonBean jsonBean = new PageJsonBean();
        List currentDrainageList= externalCrsnEqptServiceImpl.queryEqptCurrentDrainage(eqptQuery);
        jsonBean.setList(currentDrainageList);
        AjaxSupport.sendSuccessText("success", "yyyy-MM-dd", jsonBean);
    }


    /**
     * 导出设备基础信息
     */
    @RequestMapping("/queryEqptDataExl.json")
    @ResponseBody
    public void queryEqptDataExl(EqptQueryVO eqptQuery, EqptTypeEnum eqptType, HttpServletResponse response) throws IOException, ParseException {
        Asserts.assertTrue(eqptType != null, "没有获取到需要导出的设备类型");
        Workbook workbook = externalCrsnEqptServiceImpl.queryEqptDataExl(eqptQuery,eqptType);
        DownExcelUtil.fillWorkbookHead(workbook);
        workbook.write(response.getOutputStream());
    }

    /**
     * 查询设备是否有整改记录和运行记录
     * @param list
     * @return

    private List queryIsRectify(List<Object> list, int methodIndex){
    List list_t = new ArrayList(list);
    list.clear();
    for(Object t: list_t){
    Map<String, Object> map =(Map<String, Object>) JSON.toJSON(t);
    //设备整改信息
    EqptCpRecRecodeVO recRecode = new EqptCpRecRecodeVO();
    recRecode.setEqptId((String) map.get("id"));
    List<CpRecRecodeVO> recList = eqptRecodeRectifyServiceImpl.queryCpRecRecodeByEqptId(recRecode);
    if(null != recList && recList.size()>0){
    map.put("isHavRec",true);
    }else{
    map.put("isHavRec",false);
    }
    //设备运行记录
    OperatorRecordsQueryVO eqptQueryVO = new OperatorRecordsQueryVO();
    eqptQueryVO.setDeviceId((String) map.get("id"));
    List recordsList = operationRecordsService.queryRecodes(eqptQueryVO,methodIndex);
    if(null != recordsList && recordsList.size()>0){
    map.put("isHavRecord",true);
    }else{
    map.put("isHavRecord",false);
    }
    list.add(map);
    }
    return list;
    }*/

}
